package com.example.movie.data.cinema.model.vm

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.forward.androids.utils.ThreadUtil
import com.example.movie.R
import com.example.movie.common.constants.LoadingState
import com.example.movie.common.dao.SimpleMovieData
import com.example.movie.common.entity.Agent
import com.example.movie.common.entity.Plan
import com.example.movie.common.model.RequestResult
import com.example.movie.common.model.Result
import com.example.movie.data.cinema.model.CinemaRepository
import com.example.movie.data.cinema.model.vo.CinemaDetailVo
import com.example.movie.data.cinema.model.vo.CinemaVo

class CinemaViewModel(val repository: CinemaRepository) : ViewModel() {

    private val _cinemas = MutableLiveData<RequestResult<CinemaVo>>()
    val cinemas: MutableLiveData<RequestResult<CinemaVo>>
        get() = _cinemas

    private val _agentInfo = MutableLiveData<RequestResult<Agent>>()
    val agent: MutableLiveData<RequestResult<Agent>>
        get() = _agentInfo

    private val _defaultPlanVo = MutableLiveData<RequestResult<List<Plan>>>()
    val defaultPlanVo: MutableLiveData<RequestResult<List<Plan>>>
        get() = _defaultPlanVo


    /**
     * 选中的轮播图对应的movieId
     */
    val movieId = MutableLiveData<Int>()

    /**
     * 选中的排片日期
     */
    val selectedPlanDay = MutableLiveData<String>()
    val planMovieImages = MutableLiveData<RequestResult<List<SimpleMovieData>>>()
    val planDates = MutableLiveData<RequestResult<List<String>>>()

    fun loadCinemaDataByHallType(
        cityId: String,
        hallTypeId: Int,
        price: Int,
        star: Int
    ) {
        var result: Result<CinemaVo>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        _cinemas.value = RequestResult(
                            success = (result as Result.Success<CinemaVo>).data
                        )
                    } else {
                        _cinemas.value = RequestResult(
                            error = R.string.request_fail
                        )
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.getCinemaData(cityId, hallTypeId, price, star)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }

    }

    fun loadPlanData(agentId: Int) {
        var result: Result<CinemaDetailVo>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        planMovieImages.value = RequestResult(
                            success = (result as Result.Success<CinemaDetailVo>).data?.planMovies
                        )
                        _agentInfo.value = RequestResult(
                            success = (result as Result.Success<CinemaDetailVo>).data?.agent
                        )
                        _defaultPlanVo.value = RequestResult(
                            success = (result as Result.Success<CinemaDetailVo>).data?.planVos
                        )

                    } else {
                        _agentInfo.value = RequestResult(
                            error = R.string.request_fail
                        )
                        _defaultPlanVo.value = RequestResult(
                            error = R.string.request_fail
                        )
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.getCineMoviePlanData(agentId)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }


    /**
     * 切换电影，加载排片日期
     */
    fun changeMovie() {
        var pds: RequestResult<List<String>>? = null
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    planDates.value = pds
                }
            }
        }

        ThreadUtil.getInstance().execute {
            pds = RequestResult(success = repository.getMoviePlanDateById(movieId.value!!))
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }

    /**
     * 改变排片日期
     */
    fun changePlanDate() {
        var dpv: RequestResult<List<Plan>>? = null
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    defaultPlanVo.value = dpv
                }
            }
        }

        ThreadUtil.getInstance().execute {
            movieId.value?.let {
                selectedPlanDay.value?.let {
                    dpv = RequestResult(
                        success = repository.getMoviePlan(
                            movieId.value!!,
                            selectedPlanDay.value!!
                        )
                    )
                }
            }

            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }

    }

    fun getCinemaByCondition(price: Int, star: Int) {
        var result: Result<List<Agent>>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        val value = _cinemas.value
                        value!!.success!!.agentList = (result as Result.Success<List<Agent>>).data
                        _cinemas.value = value
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute {
            result = repository.getCinemaByCondition(price, star)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }

    }

}